package com.typing.battle.util;

import android.content.Intent;

public class MessageParser {
	private final int HEADER_INDEX = 0;
	private final int BODY_INDEX = 1;
	private final int TAIL_INDEX = 2;
	
	private static final String SEPARATOR = "|`";
	public static final int TYPE_MESSAGE = 0;
	public static final int TYPE_INVITE = 1;
	public static final int TYPE_NOTIFY = 2;
	public static final int TYPE_INOUT = 3;
	public static final int TYPE_TYPING_RESULT = 4;

	
	private String header;
	private String body;
	private String tail;
	private String packet;
	
	private Intent intent;
	
	
	private int type = 0;
	private String msg;
	private String from;
	
	public MessageParser(Intent intent){
		this.intent = intent;
		this.type = intent.getExtras().getInt("type");
		this.msg = intent.getExtras().getString("msg");
		this.from = intent.getExtras().getString("from");
	}
	
	public static String getMessageData(String message,String phoneNum){
		return TYPE_MESSAGE+SEPARATOR+message+SEPARATOR+phoneNum;
	}
	
	public static String inviteToPackey(String invite,String phoneNum){
		return TYPE_INVITE+SEPARATOR+invite+SEPARATOR+phoneNum;
	}
	
	public static String notifyToPackey(String notify){
		return TYPE_NOTIFY+SEPARATOR+notify;
	}
	
	public int getHeader(){		
		int result = -1;
		try{
			if(header != null && !"".equals(header)){
				if(TYPE_MESSAGE == Integer.parseInt(header)){
					result = TYPE_MESSAGE;
				}else if(TYPE_INVITE == Integer.parseInt(header)){
					result = TYPE_INVITE;
				}else if(TYPE_NOTIFY == Integer.parseInt(header)){
					result = TYPE_NOTIFY;
				}else if(TYPE_INOUT == Integer.parseInt(header)){
					result = TYPE_INOUT;
				}else if(TYPE_TYPING_RESULT == Integer.parseInt(header)){
					result = TYPE_TYPING_RESULT;
				}
			}
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		return result;
	}
	
	public String getBody(){
		return body;
	}
	
	public String getTail(){
		return tail;
	}
}
